package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.yddArticle.YddArticleClone;
import com.itheima.vo.R;
import com.itheima.dto.page.MyPage;
import com.itheima.dto.yddArticle.YddArticleDto;
import com.itheima.entity.YddArticle;
import com.itheima.entity.YddCate;
import com.itheima.service.YddArticleService;
import com.itheima.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangpei
 * @ClassName yddArticleController
 * @date 2022/5/30/9:40
 */
@RequestMapping("/ydd_article")
@RestController
@Slf4j
public class yddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    @Autowired
    private YddCateService yddCateService;

    /**
     * 文章分页数据展示
     *
     * @return
     */
    @GetMapping("/list")
    public R<MyPage<YddArticleDto>> list(@RequestParam(value = "start_time") Long startTime,
                                         @RequestParam(value = "end_time") Long endTime,
                                         String title, Integer cateid, Integer page, Integer limit) {
        Page<YddArticle> yddArticlePage = new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(startTime != null, YddArticle::getCreateDate, startTime, endTime)
                .like(title != null, YddArticle::getTitle, title)
                .eq(cateid != null, YddArticle::getCateid, cateid);
        yddArticleService.page(yddArticlePage, queryWrapper);
        //设置MyPage的基本属性
        MyPage<YddArticleDto> myPage = new MyPage<>();
        //总条数
        myPage.setCount(yddArticlePage.getTotal());
        //每页条数
        myPage.setPageSize(yddArticlePage.getSize());
        //总页数
        myPage.setTotalPages((yddArticlePage.getTotal() / yddArticlePage.getSize()) + 1);
        //当前页
        myPage.setCurrentPage(yddArticlePage.getCurrent());

        List<YddArticle> yddArticleList = yddArticlePage.getRecords();
        ArrayList<YddArticleDto> yddArticleDtoList = new ArrayList<>();
        for (YddArticle yddArticle : yddArticleList) {
            YddArticleDto yddArticleDto = new YddArticleDto();
            BeanUtils.copyProperties(yddArticle, yddArticleDto);
            LambdaQueryWrapper<YddCate> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(yddArticle.getCateid() != null, YddCate::getId, yddArticle.getCateid());
            YddCate one = yddCateService.getOne(wrapper);
            yddArticleDto.setCatename(one.getCatename());
            yddArticleDtoList.add(yddArticleDto);
        }
        myPage.setData(yddArticleDtoList);
        return R.success(myPage);
    }

    /**
     * 文章新增
     *
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddArticle yddArticle) {
        yddArticleService.save(yddArticle);
        return R.success(null);
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddArticleClone> rowInfo(Integer id){
        YddArticle article = yddArticleService.getById(id);
        String tags = article.getTags();
        String[] tagList = tags.split(",");
        YddArticleClone yddArticleClone = new YddArticleClone();
        BeanUtils.copyProperties(article,yddArticleClone,"tags");
        yddArticleClone.setTags(tagList);
        return R.success(yddArticleClone);
    }

    /**
     * 文章删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<YddArticle> del(Integer id){
        yddArticleService.removeById(id);
        return R.success(null);
    }

    /**
     * 文章修改
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddArticle yddArticle) {
        yddArticleService.updateById(yddArticle);
        return R.success(null);
    }
}
